<?php
namespace frontend\models\question;

use Yii;
use yii\db\ActiveRecord;
use frontend\models\question\Questionlabel;
class Question extends ActiveRecord{

	/**
	 * @inheritdoc
	 */
	public static function tableName()
	{
		return "edu_question";
	}
	//查询问题表，用户表，课程表,标签关系表，标签表
	public function Ques($pages,$orderby){
		$model = $this
			->find()
			->select(["*,qs.ques_id,GROUP_CONCAT(gs.queslabel_name SEPARATOR ' / ') AS queslabel_name"])
			->from("edu_question as qs")
			->leftjoin("edu_user","qs.user_id=edu_user.user_id")
			->leftjoin("edu_question_label as ab","qs.ques_id=ab.ques_id")
			->leftjoin("edu_queslabel as gs","ab.queslabel_id=gs.queslabel_id")
			->leftjoin("edu_course","qs.course_id=edu_course.course_id")
			->where("qs.ques_answersum!=0 and qs.ques_type=0")
			->groupby("qs.ques_id")
			->orderby($orderby)
			->offset($pages->offset)
			->limit($pages->limit)
			->asArray()
			->all();
		return $model;
	}
	//查询问题表
	public function Labels(){
		return Questionlabel::find()->asArray()->all();
		 
	}
	/**
	 * @inheritdoc 热门问题
	 */
	public function HotIssure($num)
	{
		return $this->find()->limit($num)->orderby("ques_viewsum desc")->asarray()->all();
	}

	/**
	 * @inheritdoc 问题详情
	 */
	public function Prodetails($ques_id)
	{
		return $this->find()
					->select(["*,qs.ques_id,GROUP_CONCAT(gs.queslabel_name SEPARATOR ' / ') AS queslabel_name"])
					->from("edu_question as qs")
					->leftjoin("edu_user","qs.user_id=edu_user.user_id")
					->leftjoin("edu_question_label as ab","qs.ques_id=ab.ques_id")
					->leftjoin("edu_queslabel as gs","ab.queslabel_id=gs.queslabel_id")
					->leftjoin("edu_course","qs.course_id=edu_course.course_id")
					->where("qs.ques_id=$ques_id")
					->groupby("qs.ques_id")
					->asArray()
					->one();
	}

	/**
	 * @inheritdoc 等待回复、课程问答查询
	 */
	public function Wait($pages,$type)
	{
		return $this
			->find()
			->select(["*,qs.ques_id,GROUP_CONCAT(gs.queslabel_name SEPARATOR ' / ') AS queslabel_name"])
			->from("edu_question as qs")
			->leftjoin("edu_user","qs.user_id=edu_user.user_id")
			->leftjoin("edu_question_label as ab","qs.ques_id=ab.ques_id")
			->leftjoin("edu_queslabel as gs","ab.queslabel_id=gs.queslabel_id")
			->leftjoin("edu_course","qs.course_id=edu_course.course_id")
			->where($type)
			->orderby("qs.ques_addtime desc")
			->groupby("qs.ques_id")
			->offset($pages->offset)
			->limit($pages->limit)
			->asArray()
			->all();
	}

	/**
	 * @inheritdoc 标签数量
	 */
	public function CountSum($label_id)
	{
		return $this->find()
			->select("*,qs.ques_id")
			->from("edu_question as qs")
			->leftjoin("edu_user","qs.user_id=edu_user.user_id")
			->leftjoin("edu_question_label as ab","qs.ques_id=ab.ques_id")
			->leftjoin("edu_queslabel","ab.queslabel_id=edu_queslabel.queslabel_id")
			->leftjoin("edu_course","qs.course_id=edu_course.course_id")
			->where("ab.queslabel_id=$label_id")
			->count();
	}

	/**
	 * @inheritdoc 标签搜索
	 */
	public function LabelSearch($pages,$label_id)
	{
		return $this->find()
			->select("*,qs.ques_id")
			->from("edu_question as qs")
			->leftjoin("edu_user","qs.user_id=edu_user.user_id")
			->leftjoin("edu_question_label as ab","qs.ques_id=ab.ques_id")
			->leftjoin("edu_queslabel","ab.queslabel_id=edu_queslabel.queslabel_id")
			->leftjoin("edu_course","qs.course_id=edu_course.course_id")
			->where("ab.queslabel_id=$label_id")
			->offset($pages->offset)
			->limit($pages->limit)
			->asArray()
			->all();
	}
	/**
	 * @inheritdoc rules
	 */
	public $verifyCode;
	public function rules()
	{
		return [
            ['verifyCode', 'required'],
            ['verifyCode', 'captcha', 'captchaAction' => 'questions/captcha'],
        ];
	}
	/**
	 * @inheritdoc 设置attributeLabels
	 */
	public function attributeLabels()
	{
		return [
			'verifyCode'=>'',
		];
	}

	//查询问题表，用户表，课程表,标签关系表，标签表
	static public function Question($question){
		return Question::find()
			->select(["*,qs.ques_id,GROUP_CONCAT(gs.queslabel_name SEPARATOR ' / ') AS queslabel_name"])
			->from("edu_question as qs")
			->leftjoin("edu_user","qs.user_id=edu_user.user_id")
			->leftjoin("edu_question_label as ab","qs.ques_id=ab.ques_id")
			->leftjoin("edu_queslabel as gs","ab.queslabel_id=gs.queslabel_id")
			->leftjoin("edu_course","qs.course_id=edu_course.course_id")
			->where("qs.ques_id in (".substr($question,1).")")
			->groupby("qs.ques_id")
			->orderby("qs.ques_addtime desc")
			->limit(8)
			->asArray()
			->all();
	}
}
?>